package ewms.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import ewms.bo.EwmsIndexQueryBo;
import ewms.mapper.EwmsIndexMapper;
import ewms.service.IEwmsIndexService;
import ewms.vo.EwmsIndexVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EwmsIndexServiceImpl implements IEwmsIndexService {

	@Autowired
	private EwmsIndexMapper indexMapper;

	private EwmsIndexQueryBo bo = new EwmsIndexQueryBo();
	{
		bo.setYear(LocalDate.now().getYear());
	}

	@Override
	public Map<String, BigDecimal> selectAllItemTotal() {
		List<EwmsIndexVo.AllItemTotal> allItemTotalList = indexMapper.selectAllItemTotal(bo);
		Map<String, BigDecimal> map = new HashMap<>();
		allItemTotalList.stream()
			.forEach(allItemTotal -> map.put(allItemTotal.getCountType(), allItemTotal.getWeight()));
		return map;
	}

	@Override
	public Map<String, List> selectLineChart() {
		LocalDateTime now = LocalDateTime.now();
		String endDate = DateUtil.format(now, DatePattern.PURE_DATE_PATTERN);
		String startDate = DateUtil.format(now.minusDays(30), DatePattern.PURE_DATE_PATTERN);
		bo.setStartDate(startDate);
		bo.setEndDate(endDate);
		List<EwmsIndexVo.LineChart> lineChartList = indexMapper.selectLineChart(bo);
		List<String> dateList = lineChartList.stream().map(lineChart -> lineChart.getDayShortDesc()).collect(Collectors.toList());
		List<BigDecimal> entryWasteWeightList = lineChartList.stream().map(lineChart -> lineChart.getEntryWasteWeight()).collect(Collectors.toList());
		List<BigDecimal> outWasteWeightList = lineChartList.stream().map(lineChart -> lineChart.getOutWasteWeight()).collect(Collectors.toList());
		Map<String, List> map = new HashMap<>();
		map.put("dateList", dateList);
		map.put("entryWasteWeightList", entryWasteWeightList);
		map.put("outWasteWeightList", outWasteWeightList);
		return map;
	}

	@Override
	public Map<String, Object> selectPieChart() {
		List<EwmsIndexVo.WasteWeight> pieChartList = indexMapper.selectPieChart(bo);
		List<String> wasteList = new ArrayList<>();
		List<Map<String, Object>> pieDataList = pieChartList.stream()
			.peek(wasteWeight -> wasteList.add(wasteWeight.getWasteTypeName()))
			.map(wasteWeight -> {
				Map<String, Object> itemMap = new HashMap<>();
				itemMap.put("name", wasteWeight.getWasteTypeName());
				itemMap.put("value", wasteWeight.getWasteWeight());
				return itemMap;
			}).collect(Collectors.toList());
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("pieData", pieDataList);
		resultMap.put("wasteList", wasteList);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectBarChart() {
		List<EwmsIndexVo.WasteWeight> barChartList = indexMapper.selectBarChart(bo);
		List<String> wasteList = new ArrayList<>();
		List<BigDecimal> wasteWeightList = new ArrayList<>();
		barChartList.stream().forEach(wasteWeight -> {
			wasteList.add(wasteWeight.getWasteTypeName());
			wasteWeightList.add(wasteWeight.getWasteWeight());
		});
		Map<String, Object> map = new HashMap<>();
		map.put("wasteList", wasteList);
		map.put("wasteWeightList", wasteWeightList);
		return map;
	}
}
